package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.CompanyEntity;
import com.loan.system.domain.ExamineOverDataEntity;
import com.loan.system.mapper.ApplyMapper;
import com.loan.system.mapper.ExamineOverDataMapper;
import com.loan.system.service.ExamineOverDataService;
import io.swagger.annotations.Example;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author shiguang on 2022/10/9 14:13
 */
@Service
@RequiredArgsConstructor
public class ExamineOverDataServiceImpl implements ExamineOverDataService {


    private final ExamineOverDataMapper examineOverDataMapper;

    private final ApplyMapper applyMapper;

    @Override
    public void addOrUpdateExamineOverData() {
        //获取所有的初审用户及数据
        List<Map<String, Object>> oneList = applyMapper.selectOneExamineData();
        for (Map<String, Object> map : oneList) {
            ExamineOverDataEntity examineOverDataEntity = new ExamineOverDataEntity();
            examineOverDataEntity.setUserId((Long) map.get("userId"));
            examineOverDataEntity.setUserName((String) map.get("userName"));
            examineOverDataEntity.setType("0");
            examineOverDataEntity.setExamineNum(Integer.valueOf(map.get("num").toString()));
            examineOverDataEntity.setExaminePassNum(Integer.valueOf(map.get("num1").toString()));
            Integer num2 = Integer.valueOf(map.get("num2").toString());
            Integer num3 = Integer.valueOf(map.get("num3").toString());
            Integer num4 = Integer.valueOf(map.get("num4").toString());
            Integer totalNum = num2 + num3 + num4;
            examineOverDataEntity.setFirstOverNum(totalNum);
            examineOverDataEntity.setSevenNum(Integer.valueOf(map.get("num5").toString()));
            examineOverDataEntity.setAfterSevenNum(Integer.valueOf(map.get("num6").toString()));
            examineOverDataEntity.setPayNum(Integer.valueOf(map.get("num7").toString()));
            examineOverDataEntity.setApplyMoney(Long.valueOf(map.get("money").toString()));
            examineOverDataEntity.setRepayMoney(Long.valueOf(map.get("repayMoney").toString()));
            ExamineOverDataEntity entity = examineOverDataMapper.selectOne(new QueryWrapper<ExamineOverDataEntity>().eq("user_id", examineOverDataEntity.getUserId()).eq("type", "0"));
            if (entity!= null){
                examineOverDataMapper.delete(new QueryWrapper<ExamineOverDataEntity>().eq("user_id", examineOverDataEntity.getUserId()).eq("type", "0"));
            }
            examineOverDataMapper.insert(examineOverDataEntity);
        }

        List<Map<String, Object>> twoList = applyMapper.selectTwoExamineData();
        for (Map<String, Object> map : twoList) {
            ExamineOverDataEntity examineOverDataEntity = new ExamineOverDataEntity();
            examineOverDataEntity.setUserId((Long) map.get("userId"));
            examineOverDataEntity.setUserName((String) map.get("userName"));
            examineOverDataEntity.setType("1");
            examineOverDataEntity.setExamineNum(Integer.valueOf(map.get("num").toString()));
            examineOverDataEntity.setExaminePassNum(Integer.valueOf(map.get("num1").toString()));
            Integer num2 = Integer.valueOf(map.get("num2").toString());
            Integer num3 = Integer.valueOf(map.get("num3").toString());
            Integer num4 = Integer.valueOf(map.get("num4").toString());
            Integer totalNum = num2 + num3 + num4;
            examineOverDataEntity.setFirstOverNum(totalNum);
            examineOverDataEntity.setSevenNum(Integer.valueOf(map.get("num5").toString()));
            examineOverDataEntity.setAfterSevenNum(Integer.valueOf(map.get("num6").toString()));
            examineOverDataEntity.setPayNum(Integer.valueOf(map.get("num7").toString()));
            examineOverDataEntity.setApplyMoney(Long.valueOf(map.get("money").toString()));
            examineOverDataEntity.setRepayMoney(Long.valueOf(map.get("repayMoney").toString()));
            ExamineOverDataEntity entity = examineOverDataMapper.selectOne(new QueryWrapper<ExamineOverDataEntity>().eq("user_id", examineOverDataEntity.getUserId()).eq("type", "1"));
            if (entity!= null){
                examineOverDataMapper.delete(new QueryWrapper<ExamineOverDataEntity>().eq("user_id", examineOverDataEntity.getUserId()).eq("type", "1"));
            }
            examineOverDataMapper.insert(examineOverDataEntity);
        }
    }

    @Override
    public TableDataInfo<ExamineOverDataEntity> selectPageExamineOverDataList(String beginTime, String endTime, String type, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamineOverDataEntity> lqw = new LambdaQueryWrapper<ExamineOverDataEntity>()
               .eq(StringUtils.isNotBlank(type),ExamineOverDataEntity::getType,type)
                .orderByAsc(ExamineOverDataEntity::getUserName);
        Page<ExamineOverDataEntity> page = examineOverDataMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }
}
